/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Model.Bean.PagoAgua;
import Model.Bean.PagoLuz;
import Model.Bean.PagoRenta;
import Model.Servicios.PagoAguaSrv;
import Model.Servicios.PagoLuzSrv;
import Model.Servicios.PagoRentaSrv;
import Util.Fecha;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author sixviv
 */
public class notificaciones extends HttpServlet {
    List<PagoAgua> pagosAguaPen = new ArrayList<PagoAgua>();
    List<PagoLuz> pagosLuzPen = new ArrayList<PagoLuz>();
    List<PagoRenta> pagosRentaPen = new ArrayList<PagoRenta>();
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            this.getPendientesAgua();
            this.getPendientesLuz();
            this.getPendientesRenta();
            request.getSession().setAttribute("pagoLuzPen", pagosLuzPen);
            request.getSession().setAttribute("pagosRentaPen", pagosRentaPen);
            request.getSession().setAttribute("pagosAguaPen", pagosAguaPen);
            request.getRequestDispatcher("panelSecretaria.jsp").include(request,response);
        } finally { 
            out.close();
        }
    } 
    
    private void getPendientesRenta(){
        PagoRentaSrv rSrv = new PagoRentaSrv();
        List<PagoRenta> pagosRenta = rSrv.getPagosRenta();
        for(PagoRenta pagoRenta : pagosRenta){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(pagoRenta.getFechaPago());
            Calendar calendarCurrent = Calendar.getInstance();
            calendar.setTime(new Date());
            if(Fecha.diferenciaDates(calendar, calendarCurrent)<=0){
                pagosRentaPen.add(pagoRenta);
            }
        }
    }
    
    private void getPendientesAgua(){
        PagoAguaSrv aSrv = new PagoAguaSrv();
        List<PagoAgua> pagosAgua = aSrv.getPagosAgua();
        for(PagoAgua pagoAgua : pagosAgua){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(pagoAgua.getFecha());
            Calendar calendarCurrent = Calendar.getInstance();
            calendar.setTime(new Date());
            if(Fecha.diferenciaDates(calendar, calendarCurrent)<=0){
                pagosAguaPen.add(pagoAgua);
            }
        }
    }
    
    private void getPendientesLuz(){
        PagoLuzSrv lSrv = new PagoLuzSrv();
        List<PagoLuz> pagosLuz = lSrv.getPagosLuz();
        for(PagoLuz pagoLuz : pagosLuz){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(pagoLuz.getFecha());
            Calendar calendarCurrent = Calendar.getInstance();
            calendar.setTime(new Date());
            if(Fecha.diferenciaDates(calendar, calendarCurrent)<=0){
                pagosLuzPen.add(pagoLuz);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
